package io.gitee.yth.design.pattern.test.core.pattern.behavioral.iterator;

import org.junit.jupiter.api.Test;
import io.gitee.yth.design.pattern.test.core.pattern.behavioral.iterator.code.College;
import io.gitee.yth.design.pattern.test.core.pattern.behavioral.iterator.code.Department;
import io.gitee.yth.design.pattern.test.core.pattern.behavioral.iterator.code.Organization;
import io.gitee.yth.design.pattern.test.core.pattern.behavioral.iterator.code.University;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * @author yth
 * @since 2023-04-29-17:44
 */
public class Client {

    @Test
    public void test1() {
        final MyStack<Integer> stack = new MyStack<>();
        stack.push(1);
        stack.push(2);
        stack.pop();
        stack.push(3);
        stack.push(7);
        stack.push(9);

        int[] sum = new int[]{0};
        stack.iterator().forEachRemaining(e -> sum[0] += e);
        System.out.println("sum = " + sum[0]);

        final Iterator<Integer> iterator = stack.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    @Test
    public void test2() {
        Organization csu = new University("中南大学", "末流985");
        Organization computerScience = new College("计算机学院");
        Organization autoMachine = new College("自动化学院");
        Organization cs1 = new Department("计算机科学与技术");
        Organization cs2 = new Department("信息安全");
        Organization cs3 = new Department("物联网");
        Organization am1 = new Department("机械电子工程技术");
        Organization am2 = new Department("电气工程及自动化");

        computerScience.add(cs1);
        computerScience.add(cs2);
        computerScience.add(cs3);
        autoMachine.add(am1);
        autoMachine.add(am2);
        csu.add(computerScience);
        csu.add(autoMachine);

        final Iterator<Organization> itr = cs1.iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next().getName());
        }

        System.out.println("-----------------------");

        final Iterator<Organization> iterator = csu.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().getName());
        }

        System.out.println("-----------------------");

        final Iterator<Organization> csuItr = csu.iterator();

        csuItr.next();
        csuItr.next();
        csuItr.next();
        csuItr.remove();

        while (csuItr.hasNext()) {
            System.out.println(csuItr.next().getName());
        }

        System.out.println("-----------------------");

        csu.printAll();
    }

    @Test
    public void test3() {
        final ArrayList<Object> list = new ArrayList<>();
        final HashMap<Object, Object> map = new HashMap<>();
        final LinkedList<Object> linkedList = new LinkedList<>();
        list.add(1);
        list.add(3);
        list.add(6);
        list.add(7);
        list.add(65);
        list.add(3);

        final Iterator<Object> iterator = list.iterator();
        while (iterator.hasNext()) {
            final Object next = iterator.next();
            if ((int) next < 10) {
                iterator.remove();
            }
        }

        System.out.println("list = " + list);
    }

}
